package com.example.demo.embryolisse.stephanehumbertlucas;

import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.metadata.data.HyperlinkData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.fastjson2.JSON;
import com.example.demo.ExcelUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class Exportor {
    @Test
    @SneakyThrows
    public void export() {
        String folder = "C:\\Users\\blysin\\Documents\\workspaces\\java\\demo\\src\\test\\java\\com\\example\\demo\\embryolisse\\stephanehumbertlucas\\";
        String path = "stephanehumbertlucas.json";
        String xlsxPath = "stephanehumbertlucas.xlsx";
        String json = FileUtil.readUtf8String(folder + path);
        List<WebData> list = JSON.parseArray(json, WebData.class);
        List<WebData> distinct = list.stream()
                .distinct()
                .sorted(Comparator.comparing(WebData::getContry))
                .collect(Collectors.toList());
        System.out.println(distinct.size());

        distinct.forEach(webData -> {
            webData.setDistUrl(newUrlColumn(webData.getDistWebsite()));
            webData.setPartnerUrl(newUrlColumn(webData.getPartnerWebsite()));
        });

        ExcelUtils.exportToExcel(folder + xlsxPath, distinct, WebData.class);
    }

    private static WriteCellData<String> newUrlColumn(String urlTheordinary) {
        if (urlTheordinary == null || urlTheordinary.isEmpty()) {
            return null;
        }
        WriteCellData<String> hyperlink = new WriteCellData<>(urlTheordinary);

        try {
            HyperlinkData hyperlinkData = new HyperlinkData();
            hyperlinkData.setAddress(urlTheordinary);
            hyperlinkData.setHyperlinkType(HyperlinkData.HyperlinkType.URL);
            hyperlink.setHyperlinkData(hyperlinkData);
        } catch (Exception e) {
            log.error("url错误:{}", urlTheordinary, e);
            hyperlink.setStringValue(urlTheordinary);
        }
        return hyperlink;
    }
}
